package com.example.weddingapp.base;


import android.app.Activity;
import android.widget.Toast;

import com.kaopiz.kprogresshud.KProgressHUD;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class BaseObserver<T> implements Observer<T> {
    private KProgressHUD kProgressHUD;
    private Activity mActivity;

    public BaseObserver() {
    }

    public BaseObserver(Activity activity) {
        this.mActivity = activity;
        kProgressHUD = KProgressHUD.create(activity)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE);

    }

    @Override
    public void onSubscribe(Disposable d) {
        if (mActivity != null) {
            kProgressHUD.show();
        }
    }

    @Override
    public void onNext(T t) {
        if (kProgressHUD != null && kProgressHUD.isShowing()) {
            kProgressHUD.dismiss();
        }
    }

    @Override
    public void onError(Throwable e) {

        if (kProgressHUD != null && kProgressHUD.isShowing()) {
            kProgressHUD.dismiss();
        }
        if (mActivity != null) {
            Toast.makeText(mActivity, e.getMessage(), Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onComplete() {
        if (kProgressHUD != null && kProgressHUD.isShowing()) {
            kProgressHUD.dismiss();
        }
    }
}
